﻿package com.sms.monitor;

import java.text.SimpleDateFormat;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.telephony.SmsMessage;

import com.sms.monitor.constant.Constant;
import com.sms.monitor.server.MyContentObserver;
import com.sms.monitor.server.SMSServer;
import com.sms.monitor.ui.activity.Dialog2Activity;
import com.sms.monitor.utils.SharedPreferencesUtil;
import com.sms.monitor.utils.ULog;
import com.sms.monitor.utils.Util;

/**
 * 
 * 短信拦截器，注册到系统，拦截短信。<br>
 * 开机或者网络切换，启动后台服务
 * 
 * @author dongfang
 * 
 */
public class SMSBroadcast extends BroadcastReceiver {

    /** 短信接收 */
    private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
    /** 开机结束 */
    private static final String BOOT_COMPLETED = "android.intent.action.BOOT_COMPLETED";
    /** 网络变更 */
    // private static final String CONNECTIVITY_CHANGE = "android.net.conn.CONNECTIVITY_CHANGE";
    private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        ULog.i("MSG_TYPE = " + action);
        /** 开机或者网络切换，自动启动服务 */
        if (BOOT_COMPLETED.equals(action)) {
            // ||CONNECTIVITY_CHANGE.equals(action)
            context.startService(new Intent(context, SMSServer.class));
        }

        else if (SMS_RECEIVED.equals(action)) {
            try {
                Bundle bundle = intent.getExtras();
                ULog.v("SMS_RECEIVED ");
                if (bundle != null) {
                    Object[] pdu = (Object[]) bundle.get("pdus");
                    SmsMessage[] sms = new SmsMessage[pdu.length];

                    for (int i = 0; i < pdu.length; i++) {
                        sms[i] = SmsMessage.createFromPdu((byte[]) pdu[i]);
                    }

                    for (SmsMessage msg : sms) {
                        String smsMsg = msg.getMessageBody();
                        String smsSendPhone = msg.getOriginatingAddress();

                        ULog.i("sms send phone   : " + smsSendPhone);
                        ULog.i("sms msg          : " + smsMsg);
                        ULog.i("sms msg count    : " + smsMsg.length());
                        // 判断主叫号码是否包含在拦截列表中。
                        if (Util.containsPhone(Util.converStringToSet(SharedPreferencesUtil.getValue(context,
                                Constant.KEY_SMS_SPF_PHONE)), smsSendPhone)) {

                            Intent mIntent = new Intent(context, Dialog2Activity.class);
                            mIntent.putExtra(Constant.KEY_SMS_TYPE, 2);
                            mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            mIntent.putExtra(Constant.KEY_SMS_LENGTH, String.valueOf(smsMsg.length()));
                            mIntent.putExtra(Constant.KEY_SMS_MSG, Util.replaceAllKeyWords(smsMsg, Util
                                    .converStringToSet(SharedPreferencesUtil.getValue(context,
                                            Constant.KEY_SMS_SPF_KEYWORD))));
                            mIntent.putExtra(Constant.KEY_SMS_SEND_PHONE, new MyContentObserver(new Handler(), context)
                                    .getContacts(new String[] { smsSendPhone }));
                            context.startActivity(mIntent);
                            return;
                        }

                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }
}
